package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.TradeNoUtils;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;



@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;


    /**
     * 获取订单详情
     *
     * @return
     */

//    @GetMapping("/order/{id}")
//    public ResponseData orderDetail(@PathVariable("id") String id) {
//        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
//        orderDetailRequest.setOrderId(id);
//
//        OrderDetailResponse orderDetailResponse = orderQueryService.selectOrderDetail(orderDetailRequest);
//        if (orderDetailResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
//            return new ResponseUtil().setData(orderDetailResponse);
//        }
//        return new ResponseUtil().setErrorMsg(orderDetailResponse.getMsg());
//    }
    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String id) {
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);

        OrderDetailResponseMy orderDetailResponseMy = orderQueryService.selectOrderDetail(orderDetailRequest);

        if (orderDetailResponseMy.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(orderDetailResponseMy);
        }
        return new ResponseUtil().setErrorMsg(orderDetailResponseMy.getMsg());
    }

    // /**
    //  * 取消订单
    //  *
    //  * @return
    //  */
    // @PostMapping("/cancelOrder")
    // public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
    //
    //     cancelOrderRequest.setOrderId(cancelOrderRequest.getOrderId());
    //
    //     CancelOrderResponse cancelOrderResponse =  orderQueryService.cancelOrder(cancelOrderRequest);
    //     if (cancelOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
    //         return new ResponseUtil().setData(cancelOrderResponse);
    //     }
    //     return new ResponseUtil().setErrorMsg(cancelOrderResponse.getMsg());
    // }
    // /**
    //  * 删除订单
    //  *
    //  * @return
    //  */
    // @DeleteMapping("/order/{id}")
    // public ResponseData deleteOrder(@PathVariable("id") String id) {
    //
    //     DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
    //     deleteOrderRequest.setOrderId(id);
    //
    //     DeleteOrderResponse deleteOrderResponse= orderQueryService.deleteOrder(deleteOrderRequest);
    //
    //     if (deleteOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
    //         return new ResponseUtil().setData(deleteOrderResponse);
    //     }
    //     return new ResponseUtil().setErrorMsg(deleteOrderResponse.getMsg());
    // }



    /**
     * @description:
     * @param: 创建订单
     * @return:
     * @author zhizhuke
     * @date: 2022-01-20 16:49
     */
    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest orderRequest, HttpServletRequest httpRequest) {
        String userInfo = (String) httpRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        orderRequest.setUserId(uid);
        orderRequest.setUniqueKey(TradeNoUtils.generateTradeNo());//利用单号生成器，生成全局唯一的id

        CreateOrderResponse orderResponse = orderCoreService.createOrder(orderRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(orderResponse.getCode())) {
            return new ResponseUtil().setData(orderResponse.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(orderResponse.getMsg());
    }


    @GetMapping("/order")
    public ResponseData orderList(Integer page,Integer size,String sort,HttpServletRequest httpRequest) {

        OrderListRequest listRequest = new OrderListRequest();
        String userInfo = (String) httpRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        listRequest.setPage(page);
        listRequest.setSize(size);
        listRequest.setSort(sort);
        listRequest.setUserId(uid);

        OrderListResponse listResponse = orderQueryService.listOrder(listRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(listResponse.getCode())) {
            return new ResponseUtil().setData(listResponse.getOrderListDto());
        }
        return new ResponseUtil().setErrorMsg(listResponse.getMsg());
    }
}

